﻿using Net.Server;
using Net.Share;

namespace ServerStressTesting
{
    public class P : UdxPlayer
    {
        public override void OnUpdate()
        {
            if (Scene == null)
                return;
            //(Scene as NetScene<p>).AddOperation(new Operation(5, new byte[28]));//压测并发, 每个客户端一次70多个byte数据
        }
    }

    public class S : SceneBase<P>
    {
        public override void OnOperationSync(P client, in OperationList list)
        {
            AddOperations(list.operations);
        }
        public override void OnExit(P client)
        {
            if (Count <= 0)
                ServerStressTesting.Server.Instance.RemoveScene(Name, false);
        }
    }

    public class Server : TcpServer<P, S>
    {
        public static Server Instance;
        public int num;
        public int sceneID = 10000;
        protected override void OnStarting()
        {
            base.OnStarting();
            Instance = this;
        }
        protected override void OnStartupCompleted()
        {
            var s = CreateScene(sceneID.ToString());
            s.Capacity = 3;
            sceneID++;
        }
        protected override void OnAddPlayerToScene(P client)
        {
            lock (SyncRoot)
            {
                num++;
                if (num >= 3)
                {
                    var s = CreateScene(client, sceneID.ToString());
                    if (s != null)
                    {
                        s.Capacity = 3;
                    }
                    sceneID++;
                    num = 0;
                }
                else
                {
                    JoinScene(client, (sceneID - 1).ToString());
                }
            }
        }
    }
}
